package com.th.listener;

import com.th.pojo.ProductType;
import com.th.service.ProductTypeService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;
import java.util.List;

/**
 * @author ThreePure
 * @date 2022/3/27 18:06
 * @description: 商品类别监听器
 * @since 1.8
 */
@WebListener
public class ProductTypeListener  implements ServletContextListener {
    @Override
    public void contextInitialized(ServletContextEvent servletContextEvent) {
        //手动从Spring容器中获取ProductTypeService对象
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext-*.xml");
        ProductTypeService productTypeService = (ProductTypeService) context.getBean("ProductTypeServiceImpl");
        List<ProductType> typeList = productTypeService.getAll();
        //存入全局作用域中，供新增页面，修改页面，前台查询功能提供全部商品类别集合
        typeList.forEach( list -> System.out.println("【类别】"+list));
        servletContextEvent.getServletContext().setAttribute("typeList",typeList);
    }

    @Override
    public void contextDestroyed(ServletContextEvent servletContextEvent) {

    }
}
